package com.example.security.config;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;

// @Configuration
@Slf4j
public class RabbitMqConfig {

  public MessageListener listener() {
    return new MessageListener() {

      @SneakyThrows
      @Override
      public void onMessage(Message message) {
        // 收到消息后如何处理
        log.info("Receive msg: " + message.toString());
        Thread.sleep(10 * 1000L);
        throw new RuntimeException("ooo");
      }
    };
  }

  @Bean
  public SimpleMessageListenerContainer simpleMessageListenerContainer(
      ConnectionFactory connectionFactory) {
    var container = new SimpleMessageListenerContainer();
    container.setMessageListener(listener());
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setQueueNames("morning");
    container.setConnectionFactory(connectionFactory);
    return container;
  }

  @Bean
  public SimpleMessageListenerContainer simpleMessageListenerContainer1(
      ConnectionFactory connectionFactory) {
    var container = new SimpleMessageListenerContainer();
    container.setMessageListener(listener());
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setQueueNames("morning");
    container.setConnectionFactory(connectionFactory);
    return container;
  }
}
